<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出售人" prop="sellName">
        <el-input
          v-model="queryParams.sellName"
          placeholder="请输入出售人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="买方" prop="buyName">
        <el-input
          v-model="queryParams.buyName"
          placeholder="请输入买方"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="state">
        <el-select
          v-model="queryParams.state"
          placeholder="请选择"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="item in orderState"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['mall:mallOfflineOrder:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="mallOfflineOrderList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单信息" align="center" width="200">
        <template #default="scope">
          <p>订单编号:{{ scope.row.orderNo }}</p>
          <p>订单状态:{{ stateFun(scope.row.state, orderState) }}</p>
        </template>
      </el-table-column>
      <el-table-column label="产品信息" align="center" width="200">
        <template #default="scope">
          <p>产品名称:{{ scope.row.name }}</p>
          <p>产品价格:{{ scope.row.price }}</p>
        </template>
      </el-table-column>
      <el-table-column label="让利比例" align="center" prop="discountRate" />
      <el-table-column label="支付信息" align="center" width="200">
        <template #default="scope">
          <p>支付单号:{{ scope.row.tradeNo }}</p>
          <p>支付时间:{{ parseTime(scope.row.payTime) }}</p>
          <p>支付方式:{{ stateFun(scope.row.payWay, payWay) }}</p>
          <p>实际支付金额:{{ scope.row.payPrice }}</p>
        </template>
      </el-table-column>
      <el-table-column
        label="出售人"
        align="center"
        prop="sellUserInfo.userName"
      />
      <el-table-column
        label="买方"
        align="center"
        prop="buyUserInfo.userName"
      />
      <el-table-column label="店铺名称" align="center" prop="storeInfo.name" />
      <el-table-column
        label="出售时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="MallOfflineOrder">
import {
  listMallOfflineOrder,
  getMallOfflineOrder,
  delMallOfflineOrder,
  addMallOfflineOrder,
  updateMallOfflineOrder,
} from "@/api/mall/mallOfflineOrder";

const { proxy } = getCurrentInstance();

const mallOfflineOrderList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const orderState = ref([
  {
    label: "待付款",
    value: 0,
  },
  {
    label: "已完成",
    value: 1,
  },
]);

const payWay = ref([
  {
    label: "微信",
    value: 0,
  },
  {
    label: "微信",
    value: 1,
  },
  {
    label: "支付宝",
    value: 2,
  },
  {
    label: "余额",
    value: 5,
  },
]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNo: null,
    name: null,
    price: null,
    discountRate: null,
    state: null,
    payTime: null,
    payWay: null,
    tradeNo: null,
    sellBy: null,
    sellTime: null,
    sellName: null,
    buyName: null,
    storeName: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询线下订单列表 */
function getList() {
  loading.value = true;
  listMallOfflineOrder(queryParams.value).then((response) => {
    mallOfflineOrderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    orderNo: null,
    name: null,
    price: null,
    discountRate: null,
    state: null,
    payTime: null,
    payWay: null,
    tradeNo: null,
    createBy: null,
    createTime: null,
    sellBy: null,
    sellTime: null,
  };
  proxy.resetForm("mallOfflineOrderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加线下订单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getMallOfflineOrder(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改线下订单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["mallOfflineOrderRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateMallOfflineOrder(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMallOfflineOrder(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除线下订单编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delMallOfflineOrder(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "mall/mallOfflineOrder/export",
    {
      ...queryParams.value,
    },
    `mallOfflineOrder_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
